# -*- coding: utf-8 -*-
# this file is released under public domain and you can use without limitations

#########################################################################
## This scaffolding model makes your app work on Google App Engine too
#########################################################################

if request.env.web2py_runtime_gae:            # if running on Google App Engine
    db = DAL('gae')                           # connect to Google BigTable
                                              # optional DAL('gae://namespace')
    session.connect(request, response, db = db) # and store sessions and tickets there
    ### or use the following lines to store sessions in Memcache
    # from gluon.contrib.memdb import MEMDB
    # from google.appengine.api.memcache import Client
    # session.connect(request, response, db = MEMDB(Client()))
else:                                         # else use a normal relational database
    db = DAL('sqlite://storage.sqlite')       # if not, use SQLite or other DB
## if no need for session
# session.forget()

#########################################################################
## Here is sample code if you need for
## - email capabilities
## - authentication (registration, login, logout, ... )
## - authorization (role based authorization)
## - services (xml, csv, json, xmlrpc, jsonrpc, amf, rss)
## - crud actions
## (more options discussed in gluon/tools.py)
#########################################################################

from gluon.tools import *
mail = Mail()                                  # mailer
auth = Auth(globals(),db)                      # authentication/authorization
crud = Crud(globals(),db)                      # for CRUD helpers using auth
service = Service(globals())                   # for json, xml, jsonrpc, xmlrpc, amfrpc
plugins = PluginManager()

mail.settings.server = 'logging' or 'smtp.gmail.com:465'  # your SMTP server
mail.settings.sender = ''         # your email
mail.settings.login = ''      # your credentials or None

auth.settings.hmac_key = 'sha512:6696632d-e847-4445-a914-dae73c8c29c8'   # before define_tables()
auth.define_tables()                           # creates all needed tables
auth.settings.mailer = mail                    # for user email verification
auth.settings.registration_requires_verification = False
auth.settings.registration_requires_approval = False
auth.messages.verify_email = 'Click on the link http://'+request.env.http_host+URL('default','user',args=['verify_email'])+'/%(key)s to verify your email'
auth.settings.reset_password_requires_verification = True
auth.messages.reset_password = 'Click on the link http://'+request.env.http_host+URL('default','user',args=['reset_password'])+'/%(key)s to reset your password'

#########################################################################
## If you need to use OpenID, Facebook, MySpace, Twitter, Linkedin, etc.
## register with janrain.com, uncomment and customize following
# from gluon.contrib.login_methods.rpx_account import RPXAccount
# auth.settings.actions_disabled=['register','change_password','request_reset_password']
# auth.settings.login_form = RPXAccount(request, api_key='...',domain='...',
#    url = "http://localhost:8000/%s/default/user/login" % request.application)
## other login methods are in gluon/contrib/login_methods
#########################################################################

crud.settings.auth = None                      # =auth to enforce authorization on crud

#########################################################################
## Define your tables below (or better in another model file) for example
##
## >>> db.define_table('mytable',Field('myfield','string'))
##
## Fields can be 'string','text','password','integer','double','boolean'
##       'date','time','datetime','blob','upload', 'reference TABLENAME'
## There is an implicit 'id integer autoincrement' field
## Consult manual for more options, validators, etc.
##
## More API examples for controllers:
##
## >>> db.mytable.insert(myfield='value')
## >>> rows=db(db.mytable.myfield=='value').select(db.mytable.ALL)
## >>> for row in rows: print row.id, row.myfield
#########################################################################

db.define_table(
    'subject',
    Field('name', notnull=True),
    Field('title'),
    Field('description', 'text'),
    format='%(name)s'
)

db.define_table(
    'syllabus',
    Field('year', 'integer', notnull=True),
    format='%(year)s'    
)

db.define_table(
    'course',
    Field('course', notnull=True),
    format='%(course)s'    
)

db.define_table(
    'prerequisites',
    Field('prereq', db.subject, notnull=True),
    Field('subject', db.subject, notnull=True),
    Field('year', db.syllabus, default=9),
    Field('course', 'list:reference db.course', default='13')
)

db.prerequisites.prereq.requires=IS_IN_DB(db, 'subject.id', '%(name)s')
db.prerequisites.subject.requires=IS_IN_DB(db, 'subject.id', '%(name)s')
db.prerequisites.year.requires=IS_IN_DB(db, 'syllabus.id', '%(year)s')
db.prerequisites.course.requires = IS_IN_DB(db,'course.id',db.course._format,multiple=True)

db.define_table(
    'subject_course',
    Field('subject', db.subject, notnull=True),
    Field('course', db.course, notnull=True, default=13),
    Field('year', db.syllabus, default=9)
)
db.subject_course.subject.requires=IS_IN_DB(db, 'subject.id', '%(name)s')
db.subject_course.course.requires=IS_IN_DB(db, 'course.id', '%(course)s')
db.subject_course.year.requires=IS_IN_DB(db, 'syllabus.id', '%(year)s')

db.define_table(
    'corequisites',
    Field('coreq', db.subject, notnull=True),
    Field('subject', db.subject, notnull=True),
    Field('year', db.syllabus, default=9),
    Field('course', 'list:reference db.course', default='13')
)
db.corequisites.coreq.requires=IS_IN_DB(db, 'subject.id', '%(name)s')
db.corequisites.subject.requires=IS_IN_DB(db, 'subject.id', '%(name)s')
db.corequisites.year.requires=IS_IN_DB(db, 'syllabus.id', '%(year)s')
db.corequisites.course.requires=IS_IN_DB(db, 'course.id',db.course._format,multiple=True)

db.define_table(
    'subject_requirements',
    Field('subject', db.subject, notnull=True),
    Field('requirements', notnull=True),
    Field('course', 'list:reference db.course', default='13')    
)
db.subject_requirements.subject.requires=IS_IN_DB(db, 'subject.id', '%(name)s')
db.subject_requirements.course.requires=IS_IN_DB(db, 'course.id',db.course._format,multiple=True)

db.define_table(
    'comments',
    Field('name', notnull=True),
    Field('email', notnull=True),
    Field('comment', 'text', notnull=True),
    Field('timestamp', 'datetime', default=request.now, notnull=True),
)
db.comments.timestamp.writable=db.comments.timestamp.readable=False

db.define_table(
    'domains',
    Field('domain', notnull=True),
    format='%(domain)s'
)

db.define_table(
    'ge_domain',
    Field('ge', db.subject, notnull=True),
    Field('domain', db.domains, notnull=True, default=3),
)
db.ge_domain.ge.requires = IS_IN_DB(db, 'subject.id', '%(name)s')
db.ge_domain.domain.requires = IS_IN_DB(db, 'domains.id', '%(domain)s')

db.define_table(
    'phil_stud',
    Field('ge', db.subject, notnull=True),
)
db.phil_stud.ge.requires = IS_IN_DB(db, 'subject.id', '%(name)s')

db.define_table(
    'english',
    Field('ge', db.subject, notnull=True),
)
db.english.ge.requires = IS_IN_DB(db, 'subject.id', '%(name)s')


db.define_table(
    'notes',
    Field('note', 'text', notnull=True),
    Field('course', 'list:reference db.course', default='7')
)
db.notes.course.requires = IS_IN_DB(db,'course.id',db.course._format,multiple=True)

db.define_table(
    'notes_curriculum',
    Field('note', 'text', notnull=True),
    Field('course', 'list:reference db.course', default='7')
)
db.notes_curriculum.course.requires = IS_IN_DB(db,'course.id',db.course._format,multiple=True)

db.define_table(
    'changelog',
    Field('changelog', notnull=True),
    Field('timestamp', 'datetime', default=request.now, notnull=True),
)
db.changelog.timestamp.writable=db.comments.timestamp.readable=False
